<?php
/**
 * Created by PhpStorm.
 * User: sunshubo
 * Date: 2017/12/20
 * Time: 21:50
 */

class OnLogin_control extends MY_Controller
{
    function index()
    {
        $get = $this->input->get();
        $code = $get['code'];
        $post = $this->input->raw_input_stream;
        $post = json_decode($post);
        if ($post->s === 'token') {//获取access_token
            /*
             * Token 查询步骤
             * 1、查数据库是否过期
             * 2、从网络获取
             * 3、更新数据库
             * 4、返回给前端
             * */

            $ws_token = $this->getToken();

            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxaaf5f79c147f6f2e&secret=972fe9dbf16243b9fab1faef06eb7900";
            $res = file_get_contents($url);
            $res = json_decode($res);
            echo($res->access_token);
        } else {//获取openid
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=wxaaf5f79c147f6f2e&secret=972fe9dbf16243b9fab1faef06eb7900&js_code=$code&grant_type=authorization_code";
            $res = file_get_contents($url);
            $res = json_decode($res);
            echo($res->openid);
        }
    }

    private function getToken()
    {

    }

    private function saveToken(){

    }
}